﻿using CPPEI.BD.VideoMatrixConfigTool.Datas;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CPPEI.BD.VideoMatrixConfigTool
{
    /// <summary>
    /// VideoMatrixWindow.xaml 的交互逻辑
    /// </summary>
    public partial class VideoMatrixWindow : Window
    {
        private Datas.Solution _solution;
        private Datas.Item _checkedMatrixItem;
        private Datas.Item _checkedVideoItem;
        public VideoMatrixWindow()
        {
            InitializeComponent();
            LoadVideoMatrix(null);
            LoadVideoSource(null);
        }

        public VideoMatrixWindow(Datas.Solution solution)
        {
            InitializeComponent();
            _solution = solution;
            this.DataContext = solution;
            LoadVideoMatrix(solution);
            LoadVideoSource(solution);
        }

        private void _ctlBtnSave_Click(object sender, RoutedEventArgs e)
        {
           
            Solution solution = new Solution();
            solution.Name = _ctlSolutionName.Text.Trim();
            solution.Caption = _ctlSolutionCaption.Text.Trim();
            solution.VideoMatrixId = _checkedMatrixItem.Id;
            solution.VideoSourceId = _checkedVideoItem.Id;

            if (_solution != null)
            {
                //执行更新xml信息
                if (XSolutionConfigUntil.UpdateSolution(GetSolutionFilePath(), _solution, solution))
                {
                    MessageBox.Show("修改信息成功！");
                }
                else
                {
                    MessageBox.Show("修改信息失败！");
                }
            }
            else
            {
                //执行添加xml信息
                if (XSolutionConfigUntil.AddSolution(GetSolutionFilePath(),solution))
                {
                    MessageBox.Show("添加信息成功！");
                }
                else
                {
                    MessageBox.Show("添加信息失败！");
                }

            }
        }

        private void _ctlBtnCanle_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        /// 获取solution.xml文件的地址
        /// </summary>
        /// <returns></returns>
        private static string GetSolutionFilePath()
        {
            return string.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory, "solution.xml");
        }

        /// <summary>
        /// 加载视频矩阵
        /// </summary>
        private void LoadVideoMatrix(Datas.Solution solution)
        {
            List<Item> ListVideoMatrix = XSolutionConfigUntil.GetListVideoOrMatrixSource(GetVideoMatrixFilePath(), "VideoMatrix");
            if (ListVideoMatrix != null)
            {
                _ctlWrapPanelMatrix.Children.Clear();
                foreach(Item item in ListVideoMatrix)
                {
                    RadioButton matrixRadioButton = new RadioButton();
                    matrixRadioButton.Margin = new Thickness(2);
                    matrixRadioButton.GroupName = "VideoMatrix";
                    matrixRadioButton.Content = item.Caption;
                    matrixRadioButton.DataContext = item;
                    matrixRadioButton.Checked += matrixRadioButton_Checked;
                    if (solution != null)
                    {
                        if (item.Id == solution.VideoMatrixId)
                        {
                            matrixRadioButton.IsChecked = true;
                        }
                    }

                    _ctlWrapPanelMatrix.Children.Add(matrixRadioButton);
                }
            }
            else
            {
                _ctlWrapPanelMatrix.Children.Clear();
            }
        }

        void matrixRadioButton_Checked(object sender, RoutedEventArgs e)
        {
           RadioButton radioButton =  sender as RadioButton;
           _checkedMatrixItem = radioButton.DataContext as Item;

        }
        
        /// <summary>
        /// 加载视频信息
        /// </summary>
        private void LoadVideoSource(Datas.Solution solution)
        {
            List<Item> ListVideoSource = XSolutionConfigUntil.GetListVideoOrMatrixSource(GetVideoSourceFilePath(), "VideoSource");
            if (ListVideoSource != null)
            {
                _ctlWrapPanelVideo.Children.Clear();
                foreach (Item item in ListVideoSource)
                {
                    RadioButton videoRadioButton = new RadioButton();
                    videoRadioButton.Margin = new Thickness(2);
                    videoRadioButton.GroupName = "VideoSource";
                    videoRadioButton.Content = item.Caption;
                    videoRadioButton.DataContext = item;
                    videoRadioButton.Checked += videoRadioButton_Checked;
                    if (solution != null)
                    {
                        if (item.Id == solution.VideoSourceId)
                        {
                            videoRadioButton.IsChecked = true;
                        }
                    }

                    _ctlWrapPanelVideo.Children.Add(videoRadioButton);
                }
            }
            else
            {
                _ctlWrapPanelVideo.Children.Clear();
            }
        }

        void videoRadioButton_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton radioButton =  sender as RadioButton;
            _checkedVideoItem = radioButton.DataContext as Item;
        }

      
        /// 获取VideoSource.xml文件的地址
        /// </summary>
        /// <returns></returns>
        private static string GetVideoSourceFilePath()
        {
            return string.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory, "VideoSource.xml");
        }

        /// 获取VideoMatrix.xml文件的地址
        /// </summary>
        /// <returns></returns>
        private static string GetVideoMatrixFilePath()
        {
            return string.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory, "VideoMatrix.xml");
        }
    }
}
